(in-package #:maclisp-compat.setagaya.mc)
(export '(repeat let.))

;;
(defmacro repeat (n &body body)
  (let ((lim (gensym))
        (cnt (gensym)))
    `(let ((,lim ,n))
       (dotimes (,cnt ,lim)
         ,@body))))

;; USAGE
;;(repeat 10 (princ "foo"))

;; 名前の競合を防ぐため、let.に名前を変更している。
;; 9/18
(defmacro let. (&body form)
  (destructuring-bind (binds body)
      (separate 'do form :test #'eq)
    (let ((blist (separate 'then binds :test #'eq)))
      (reduce 
       (lambda (xx res)
         `(let ,(do ((b xx (cdddr b))
                     res)
                    ((endp b) (nreverse res))
                    (let ((spec (srfi.setagaya:take b 3)))
                      (or (or (eq (cadr spec) '←)
                              (eq (cadr spec) '<-))
                          (error "LET.: Bad bind."))
                      (push `(,(car spec) ,(caddr spec)) res)))
            ,res))
       blist
       :initial-value `(progn ,@body)
       :from-end 'T) )))

;; usage
#|(let. a ← 1 then
        b ← (+ a 2) then
        c ← 3 
        d ← 4       then
        e ← () 
    (list a b c d) 
    (reverse (list a b c d)))|#

#|(LET ((A 1))
  (LET ((B (+ A 2)))
    (LET ((C 3) (D 4))
      (LET ((E NIL))
        (PROGN (LIST A B C D) (REVERSE (LIST A B C D)))))))|#